// 事件：主页向下滚触发的导航栏渐变动画
document.addEventListener('DOMContentLoaded', function () {
  const nav = document.getElementById('main-nav');
  checkScroll();
  window.addEventListener('scroll', checkScroll);

  function checkScroll() {
    const scrollPosition = window.scrollY;
    if (scrollPosition > 50) {
      nav.classList.add('scrolled');
    } else {
      // 滚动回顶部，移除scrolled类
      nav.classList.remove('scrolled');
      // nav.style.backgroundColor = 'transparent';
    }
  }
});

// 导航栏时间更新方法
document.addEventListener('DOMContentLoaded', function () {
  const dateElement = document.getElementById('current-date');
  const timeElement = document.getElementById('current-time');
  const weekDays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];

  let lastDate = '';
  let lastTime = '';

  function updateDateTime() {
    const now = new Date();

    const year = now.getFullYear();
    const month = now.getMonth() + 1;
    const day = now.getDate();
    const currentDate = `今天是${year}年${month}月${day}日`;

    const hours = String(now.getHours()).padStart(2, '0');
    const minutes = String(now.getMinutes()).padStart(2, '0');
    const seconds = String(now.getSeconds()).padStart(2, '0');
    const weekDay = weekDays[now.getDay()];
    const currentTime = `${hours}:${minutes}:${seconds} ${weekDay}`;

    if (currentDate !== lastDate) {
      dateElement.textContent = currentDate;
      lastDate = currentDate;
    }

    timeElement.textContent = currentTime;
    lastTime = currentTime;
  }

  updateDateTime();
  setInterval(updateDateTime, 1000);
});

function typeWriter(text, elementId, speed = 100) {
  const element = document.getElementById(elementId);
  let index = 0;

  // 初始状态：空文本
  element.textContent = '';
  element.classList.add('cursor'); // 添加闪烁光标动画

  function type() {
    if (index < text.length) {
      element.textContent = text.slice(0, index + 1);
      index++;
      setTimeout(type, speed); // 字符逐个延迟出现
    } else {
      // 模拟打字结束后，继续播放光标闪烁动画
      element.classList.add('cursor');
    }
  }

  type(); // 调用模拟打字方法
}

// 页面加载时，开始模拟打字动画，让标题文字逐个出现
window.onload = function () {
  const text = document.getElementById('typewriter').textContent;
  typeWriter(text, 'typewriter', 100); // 每隔100ms打一个字
};